package org.example.k8s_cloudplatform.utils;

import io.jsonwebtoken.*;

import java.util.Date;

public class JwtUtil {
    private static final String secrete = "9e7ed4ea-c07e-449c-a42c-70c10dc275e3";
    private static final long time = 1000 * 60 * 60 * 2;

    public static String createJwt(Integer id,Integer job) {
        return Jwts.builder()
                .setHeaderParam("type", "JWT")
                .setHeaderParam("alg", "HS256")
                .claim("id", id)
                .claim("job", job)
                .setExpiration(new Date(System.currentTimeMillis() + time))
                .signWith(SignatureAlgorithm.HS256, secrete)
                .compact();
    }

    public static int jobChecker(String token) {
        JwtParser jwtParser = Jwts.parser();//获取jwt的解析对象
        Claims claims = jwtParser.setSigningKey(secrete).parseClaimsJws(token).getBody();
        return claims.get("job", Integer.class);
    }

    public static int IDParser(String token) {
        JwtParser jwtParser = Jwts.parser();//获取jwt的解析对象
        Claims claims = jwtParser.setSigningKey(secrete).parseClaimsJws(token).getBody();
        return claims.get("id", Integer.class);
    }

    public static boolean outOfTime(String token) {
        try {
            Jwts.parser().setSigningKey(secrete).parseClaimsJws(token);
            return false;
        } catch (ExpiredJwtException e) {
            return true;
        }
    }
}